<?php
/**
 * 图像处理类
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-20
*/

class Image{
	private $file;			//图片地址
	private $width;			//图片长度
	private $height;		//图片高度
	private $type;			//图片类型
	private $path;			//物理路径
	private $new;			//处理后的图像句柄
	public function __construct($_file){
		$this->file = "http://127.0.0.1".$_file;
		list($this->width,$this->height,$this->type) = getimagesize($this->file);
		$this->img = $this->getFromImage($this->file,$this->type);
		$this->path = $_SERVER['DOCUMENT_ROOT'].$_file;
	}
	
	//cke专用图像处理
	public function ckeImg($new_width = 0,$new_height = 0){
		list($_water_width,$_water_height,$_water_type) = getimagesize(MARK);
		$_water = $this->getFromImage(MARK,$_water_type);
		
		if(empty($new_width) && empty($new_height)){
			$new_width = $this->width;
			$new_height = $this->height;
		}
		if(!is_numeric($new_width) || !is_numeric($new_height)){
			$new_width = $this->width;
			$new_height = $this->height;
 		}
 		if($this->width > $new_width){//通过宽度获得等比例高度
 			$new_height = ($new_width / $this->width) * $this->height;
 		}else {
			$new_width = $this->width;
			$new_height = $this->height;
		}
 		if($this->height > $new_height){//通过高度获得等比例宽度
 			$new_width = ($new_height / $this->height) * $this->width;
 		}else {
			$new_width = $this->width;
			$new_height = $this->height;
		}
 		
		$_water_x = $new_width - $_water_width;
		$_water_y = $new_height - $_water_height;
		
		
 		$this->new = imagecreatetruecolor($new_width,$new_height);
 		imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
 		if($new_width > $_water_width && $new_height > $_water_height){
 			imagecopy($this->new,$_water,$_water_x,$_water_y,0,0,$_water_width,$_water_height);
 		}
 		
		imagedestroy($_water);
		
	}
	
	
	//缩略图(百分比)
	public function thumb($_per){
		$new_width = $this->width * ($_per/100);
		$new_height = $this->height * ($_per/100);
		
		$this->new = imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
	}
	
	//等比例
	public function pro($new_width,$new_height){
		
		if($this->width < $this->height){
			$new_width = ($new_height / $this->height) * $this->width;
		}
		else{
			$new_height = ($new_width / $this->width) * $this->height;
		}
		
		$this->new = imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
	}
	
	//固定宽高容器，图像等比例,扩容填充，裁剪
	public function fixPro($new_width = 0,$new_height = 0){
		
		if(empty($new_width) && empty($new_height)){
			$new_width = $this->width;
			$new_height = $this->height;
		}
		
		if(!is_numeric($new_width) || !is_numeric($new_height)){
			$new_width = $this->width;
			$new_height = $this->height;
 		}
		
		//创建容器
		$_n_w = $new_width;
		$_n_h = $new_height;
		  
		//创建裁剪点
		$_cut_width = 0;
		$_cut_height = 0;
		
		if($this->width < $this->height){
			$new_width = ($new_height / $this->height) * $this->width;
		}
		else{
			$new_height = ($new_width / $this->width) * $this->height;
		}
		
		//宽度填充裁剪
		if($new_width < $_n_w){
			$r = $_n_w / $new_width;
			$new_width *= $r;
			$new_height *= $r;
			$_cut_height = ($new_height - $_n_h) / 2;
		}
		
		//高度填充裁剪
		if($new_height < $_n_h){
			$r = $_n_h / $new_height;//等比例因子
			$new_width *= $r;
			$new_height *= $r;
			$_cut_width = ($new_width - $_n_w) / 2;
		}
		
		
		$this->new = imagecreatetruecolor($_n_w,$_n_h);
		imagecopyresampled($this->new,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);
	
	}
	
	
	/**
	 * 根据图片类型加载
	 */
	
	private function getFromImage($_file,$_type){
		switch($_type){
			case 1:
				$img = imagecreatefromgif($_file);
			break;
			case 2:
				$img = imagecreatefromjpeg($_file);
			break;
			case 3:
				$img = imagecreatefrompng($_file);
			break;
			default:
				echo "此图片类型不被支持!";
				exit;
			break;
		}
		return $img;
	}
	
	//图像输出
	public function out(){
		imagepng($this->new,$this->path);
		imagedestroy($this->img);
		imagedestroy($this->new);
	}
	
	
	
}

?>